package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.website.web.util.CookiesUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationTemplateService destinationTemplateService;
    @Reference
    private ISearchService searchService;
    @Reference
    private IUserInfoTemplateService userInfoTemplateService;
    @Reference
    private IStrategyTemplateService strategyTemplateService;
    @Reference
    private ITravelTemplateService travelTemplateService;

    @RequestMapping("")
    public String index(Model model){
        //tcs
        List<TravelCommend> tcs = travelCommendService.getTop5();
        model.addAttribute("tcs",tcs);
        //sc
        StrategyCommend sc = strategyCommendService.getTop5().get(0);
        model.addAttribute("sc",sc);
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){
        //pageInfo
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "index/travelPageTpl";
    }

    @RequestMapping("q")
    public String search(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.DESTINATION_SEARCH:
                return this.searchDest(model,qo);
            case SearchQueryObject.STRATEGY_SEARCH:
                return this.searchStrategy(model,qo);
            case SearchQueryObject.TRAVEL_SEARCH:
                return this.searchTravel(model,qo);
            case SearchQueryObject.USERINFO_SEARCH:
                return this.searchUser(model,qo);
                default:return this.searchAll(model,qo);
        }
    }

    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> userInfoTemplates = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",userInfoTemplates);
        return "index/searchUser";
    }

    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> travelTemplates = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",travelTemplates);
        return "index/searchTravel";
    }

    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> strategyTemplates = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page",strategyTemplates);
        return "index/searchStrategy";
    }

    private String searchDest(Model model, SearchQueryObject qo) {
        DestinationTemplate dest = destinationTemplateService.findByName(qo.getKeyword());
        if(dest != null){
            List<StrategyTemplate> sts = strategyTemplateService.findByDestName(qo.getKeyword());
            List<UserInfoTemplate> uts = userInfoTemplateService.findByDestName(qo.getKeyword());
            List<TravelTemplate> ts = travelTemplateService.findByDestName(qo.getKeyword());
            SearchResult result = new SearchResult();
            long total = sts.size() + uts.size() + ts.size();
            result.setStrategys(sts);
            result.setTravels(ts);
            result.setUsers(uts);
            result.setTotal(total);
            model.addAttribute("data",result);
        }
        model.addAttribute("dest",dest);
        return "index/searchDest";
    }

    private String searchAll(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> strategyTemplates = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle", "summary");
        Page<TravelTemplate> travelTemplates = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        Page<UserInfoTemplate> userInfoTemplates = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        Page<DestinationTemplate> destinationTemplates = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        SearchResult result = new SearchResult();
        long total = strategyTemplates.getTotalElements() + travelTemplates.getTotalElements() + userInfoTemplates.getTotalElements() + destinationTemplates.getTotalElements();
        result.setUsers(userInfoTemplates.getContent());
        result.setTravels(travelTemplates.getContent());
        result.setStrategys(strategyTemplates.getContent());
        result.setDests(destinationTemplates.getContent());
        result.setTotal(total);
        model.addAttribute("data",result);
        return "index/searchAll";
    }
}
